﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Control_SelYearMonth : System.Web.UI.UserControl
{
    private bool _OnlyShowThisYear = false;
    private int _StartOffsetMonth = 12;
    private int _EndOffsetMonth = 12;
    private char  _Separator = '-';
    private string _SelectYear = "";
    private string _SelectMonth = "";
    private string _SelectYearMonth = "";
    private bool _AddNeedSeldate=true  ;


    /// <summary>
    /// 年和月之间的分割符
    /// </summary>
    public char Separator
    {
        get
        {
            return _Separator;
        }
        set
        {
            _Separator = value;
        }
    }

    /// <summary>
    /// 仅显示本年的时间
    /// </summary>
    public bool  OnlyShowThisYear
    {
        get
        {
            return _OnlyShowThisYear;
        }
        set
        {
            _OnlyShowThisYear = value;
        }
    }

    /// <summary>
    /// 控件是否可操作
    /// </summary>
    public bool Enabled
    {
        get
        {
            return YearMonth.Enabled ;
        }
        set
        {
            YearMonth.Enabled = value;
        }
    }

    /// <summary>
    /// 显示起始的时间与现在的偏离值
    /// </summary>
    public int  StartOffsetMonth
    {
        get
        {
            return _StartOffsetMonth;
        }
        set
        {
            _StartOffsetMonth = value;
        }
    }

    /// <summary>
    /// 显示终止的时间与现在的偏离值
    /// </summary>
    public int EndOffsetMonth
    {
        get
        {
            return _EndOffsetMonth;
        }
        set
        {
            _EndOffsetMonth = value;
        }
    }

    /// <summary>
    /// 当前选择的时间年值
    /// </summary>
    public string SelectYear
    {
        get
        {
            return GetSelDate(true);
        }
        set
        {
            _SelectYear = value;
            string SelDate = _SelectYear + _Separator + _SelectMonth;
            if (YearMonth.Items.FindByValue(SelDate) != null) YearMonth.SelectedValue = SelDate;
        }
    }

    /// <summary>
    /// 当前选择的时间月值
    /// </summary>
    public string SelectMonth
    {
        get
        {
            return GetSelDate(false);
        }
        set
        {
            _SelectMonth = value;
            string SelDate = _SelectYear + _Separator + _SelectMonth;
            if (YearMonth.Items.FindByValue(SelDate) != null) YearMonth.SelectedValue = SelDate;
        }
    }

    /// <summary>
    /// 当前选择的时间年-月值
    /// </summary>
    public string SelectYearMonth
    {
        get
        {
            return YearMonth.SelectedValue;
        }
        set
        {
            _SelectYearMonth = value;
            if (YearMonth.Items.FindByValue(_SelectYearMonth) != null) YearMonth.SelectedValue = _SelectYearMonth;
        }
    }

    /// <summary>
    /// 是否添加'请选择'选项
    /// </summary>
    public bool  AddNeedSeldate
    {
        get
        {
            return _AddNeedSeldate;
        }
        set
        {
            _AddNeedSeldate = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillData();
        }
        else
        {
            if (YearMonth.Items.Count == 0)
            {
                FillData();
            }
        }
  
    }

    /// <summary>
    /// 填仓数据
    /// </summary>
    private void FillData()
    {
        DateTime SelectedDate = DateTime.Today;

        if (_AddNeedSeldate == false)
        {
            if (_SelectYear.Length == 0) _SelectYear = SelectedDate.Year.ToString().Trim();
            if (_SelectMonth.Length == 0) _SelectMonth = SelectedDate.Month.ToString().Trim();
        }
        if (_OnlyShowThisYear == true)//仅显示本年的时间
        {
            if (_AddNeedSeldate == true)
            {
                // YearMonth.Items.Add(sYear + _Separator + i.ToString().Trim());
                ListItem dt = new ListItem();
                dt.Text = "-请选择-";
                dt.Value = "0";
                YearMonth.Items.Add(dt);
            }
            string sYear = SelectedDate.Year.ToString().Trim();
            for (int i = 1; i <= 12; i++)
            {
                //YearMonth.Items.Add(sYear + _Separator + i.ToString().Trim());
                ListItem dt = new ListItem();
                dt.Text = sYear + _Separator + i.ToString().Trim();
                dt.Value = sYear + _Separator + i.ToString().Trim();
                YearMonth.Items.Add(dt);
            }

        }
        else//
        {
            if (_AddNeedSeldate == true)
            {
                // YearMonth.Items.Add(sYear + _Separator + i.ToString().Trim());
                ListItem dt = new ListItem();
                dt.Text = "-请选择-";
                dt.Value = "0";
                YearMonth.Items.Add(dt);
            }

            DateTime date = SelectedDate.AddMonths(-_StartOffsetMonth);
            for (int i = 1; i <= _StartOffsetMonth + _EndOffsetMonth; i++)
            {
                // YearMonth.Items.Add(date.Year.ToString().Trim() + _Separator + date.Month.ToString().Trim());
                ListItem dt = new ListItem();
                dt.Text = date.Year.ToString().Trim() + _Separator + date.Month.ToString().Trim();
                dt.Value = date.Year.ToString().Trim() + _Separator + date.Month.ToString().Trim();
                YearMonth.Items.Add(dt);
                date = date.AddMonths(1);
            }

        }

        if (_AddNeedSeldate == false)
        {
            if (_SelectYearMonth.Length == 0)
            {
                string SelDate = _SelectYear + _Separator + _SelectMonth;
                if (YearMonth.Items.FindByValue(SelDate) != null) YearMonth.SelectedValue = SelDate;
            }
            else
            {
                if (YearMonth.Items.FindByValue(_SelectYearMonth) != null) YearMonth.SelectedValue = _SelectYearMonth;
            }
        }
        else
        {
            if (YearMonth.Items.FindByValue("0") != null) YearMonth.SelectedValue = "0";
        }
    }

   
    /// <summary>
    /// 取得选择日期
    /// </summary>
    /// <param name="isYear"></param>
    /// <returns></returns>
    private string GetSelDate(bool isYear)
    {
        string Selt = YearMonth.SelectedItem.Value;
        string RetS = "";
        if (Selt.Length > 0)
        {
            if (Selt != "0")
            {
                string[] Sp = Selt.Split(_Separator);
                if (Sp.Length > 1)
                {
                    RetS = isYear ? Sp[0] : Sp[1];
                }
            }

        }
        return RetS;
    }

}
